<?php


namespace app\common\controller;


use app\common\model\Admin as adminModel;
use app\common\model\AdminActionLog;
use app\common\model\AdminRoleOperationRel;
use app\common\model\Operation;
use thans\jwt\facade\JWTAuth;
use think\facade\Request;
use think\facade\View;

class AdminController extends BaseController {


    protected $admin_id      = 0;
    protected $admin_info    = [];
    protected $limit         = 15;
    protected $page         = 1;


    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        try{
            View::assign('shop_name', '杭州飞行猫');
            View::assign('shopHost', Request::domain());
            $auth_info = JWTAuth::auth();
            $admin_id = $auth_info['admin_id']->getValue();
            $this->admin_id = $admin_id;
            $this->admin_info = adminModel::where('admin_id',$admin_id)->findOrEmpty()->toArray();
            $this->limit = $this->request->param('limit',15);
            $this->page = $this->request->param('page',1);
        }catch (\Exception $e){
            cookie('redirect_url', Request::url(), 3600);
            return redirect(url('user/login'))->send();
        }

        $cont_name = request()->controller();
        $act_name  = request()->action();
        $operationModel = new Operation();
        //判断当前是否有权限操作
        $mrorModel = new AdminRoleOperationRel();
        $permRe    = $mrorModel->checkPerm($this->admin_id, $operationModel::MENU_MANAGE, $cont_name, $act_name);

        if (!$permRe['status']) {
            if (Request::isAjax()) {
                $err = [
                    'status' => false,
                    'data'   => '',
                    'msg'    => $permRe['msg']
                ];
                echo json_encode($err);
                die();
            } else {
                return $permRe;
            }
        }
        View::engine()->layout(false);
    }

    private $action_name     = '(未设置)';
    private $action_describe = '(未设置)';

    protected function setActionLog($name,$describe){
        $this->action_name = $name;
        $this->action_describe = $describe;
        $this->addActionLog();
    }

    private function addActionLog(){
        $data = [
            'admin_id'  => $this->admin_id,
            'admin_name' => $this->admin_info['name'],
            'ip'        => $this->request->ip(),
            'url'       => $this->request->url(),
            'action_name'      => $this->action_name,
            'action_desc'  => $this->action_describe,
            'created_at'  => TIMESTAMP,
        ];
        $model_AdminActionLog = new AdminActionLog();
        $model_AdminActionLog -> save($data);
    }



}